package Hash;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.type.ArrayType;
import javax.net.ssl.CertPathTrustManagerParameters;
import java.net.NetworkInterface;
import java.security.KeyStore;
import java.util.Objects;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.spi.CurrencyNameProvider;

/**
 * 人员类，用于存储个人唯一标识
 */
public class HashBuck2{
   static class Node{
       public int val;
       public int key;
       public Node next;
       public Node(int key, int val){
           this.val = val;
           this.key = key;
       }
   }
   public int usedSize;
   public Node[] array;
   public HashBuck2(){
       array = new Node[10];
   }

   public void put(int key, int val){
       int index = key % array.length;
       Node cur = array[index];
       while(cur != null){
           if(cur.key == key){
               cur.val = val;
               return ;
           }
           cur =cur.next;
       }
       Node node = new Node(key, val);
       node.next = array[index];
       array[index] = node;
       usedSize++;
       if(loadFactor() >= 0.75){
           resize();
       }
   }

   public void resize(){
       Node[] tempArr = new Node[array.length * 2];
       for(int i = 0; i < array.length; i++){
           Node cur = array[i];
           while(cur != null){
               int newIndex = cur.key % tempArr.length;
               Node curNext  = cur.next;
               cur.next = tempArr[newIndex];
               tempArr[newIndex] = cur;
               cur =  curNext;
           }
       }
       array = tempArr;
   }

   public double loadFactor(){
       return usedSize *1.0 /array.length;
   }

   public int get(int key){
       int index = key % array.length;
       Node cur  = array[index];
       while(cur != null){
           if(cur.key == key){
               return cur.val;
           }
       }
       return -1;
   }
}